// pages/schedule/schedule_add.js
const app = getApp()
import BaseModel from '../../utils/common.js'
var dateTimePicker = require('../../utils/dateTimePicker.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: '',//1是公司日程 2是个人日程
    id:'',
    roleCode:"",//角色，1是分总 2是员工
    str:'',
    memo:'',
    nav_title:"日程",
    activedImg:"0",
    navData:[
      {
        imgUrl:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/richenghui.png",
        activedUrl:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/richenglan.png",
        title:"日程"
      },
      {
        imgUrl:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/huifanghui.png",
        activedUrl:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/huifanglan.png",
        title:"回访"
      },
      {
        imgUrl:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/kehuhui.png",
        activedUrl:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/kehulan.png",
        title:"客户扩展"
      },
      {
        imgUrl:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/qitahui.png",
        activedUrl:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/qitalan.png",
        title:"其他"
      }
    ],
    earlyTime:"",//早会时间
    dateTimeArray1: null, //全部数组时间日期
    dateTime1: null, //当前选中时间日期
    startYear: 2000, //开始年份
    endYear: 2050 //结束年份
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      code:options.code,  //1是公司日程 2是个人日程
      id:options.id,
      roleCode:wx.getStorageSync('roleCode'),//角色，1是分总 2是员工
    })
    if(options.id){
      this.getDiaryOne();
    }
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();
    var arr = obj1.dateTime, dateArr = obj1.dateTimeArray;
    var earlyTime= dateArr[0][arr[0]]+'-'+dateArr[1][arr[1]]+'-'+dateArr[2][arr[2]]+' '+dateArr[3][arr[3]]+':'+dateArr[4][arr[4]]
    this.setData({
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      earlyTime: earlyTime
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    var earlyTime= dateArr[0][arr[0]]+'-'+dateArr[1][arr[1]]+'-'+dateArr[2][arr[2]]+' '+dateArr[3][arr[3]]+':'+dateArr[4][arr[4]]
    this.setData({
      earlyTime: earlyTime
    });
  },

  /*点击导航选择*/
  clickNav:function(e){
    var text = e._relatedInfo.anchorRelatedText;
    text = text.substr(0,text.length-1);
    this.setData({
      activedImg:e.currentTarget.dataset.index,
      nav_title:text
    })
  },
  //添加日程信息
  addDiaryModel: new BaseModel({
    url: 'addDiary',
    method: "get",
    needAjaxLoading: true,
    loadingText: "玩命加载中"
  }),
  addDiary(e) {
    this.addDiaryModel
        .fetch({
          type_id: this.data.code,  //个人还是公司日程
          memo: this.data.nav_title,  //头部nav
          title: e.detail.value.title,  //标题
          content: e.detail.value.content,//内容
          time: this.data.earlyTime,  //日程时间
        })
        .then(
        wx.switchTab({
          url: "./schedule",
          success: function (e) {
            var page = getCurrentPages().pop();
            if (page == undefined || page == null) return;
            page.onReady();
          }
        })
    )
        .catch(res => {
          //失败
        })
  },
  //获取日程信息
  getDiaryOneModel: new BaseModel({
    url: 'getDiaryOne',
    method: "get",
    needAjaxLoading: true,
    loadingText: "玩命加载中"
  }),
  getDiaryOne() {
    this.getDiaryOneModel
      .fetch({
        id: this.data.id
      })
      .then(res => {
        if(res.data.diarylog){
          var str = res.data.diarylog.address + '\n' +
            res.data.diarylog.company + '\n' +
            res.data.diarylog.contracts + '\n' +
            res.data.diarylog.estimated_number + '\n' +
            res.data.diarylog.jiangshi + '\n' +
            res.data.diarylog.phone + '\n' +
            res.data.diarylog.time + '\n' +
            res.data.diarylog.remarks + '\n'
        }else{
          var str = res.data.content
        }
        this.setData({
          memo: res.data,
          str: str,
          nav_title:res.data.memo,
          earlyTime:res.data.create_time
        }); 
        if(res.data.memo=="日程"){
          this.setData({
            activedImg: 0,
          })
        }else if(res.data.memo=="回访"){
          this.setData({
            activedImg: 1,
          })
        }else if(res.data.memo=="客户扩展"){
          this.setData({
            activedImg: 2,
          })
        }else{
          this.setData({
            activedImg: 3,
          })
        }
      })
      .catch(res => {
        //失败
      })
  },
  //更新日程信息
  updateDiaryModel: new BaseModel({
    url: 'updateDiary',
    method: "get",
    needAjaxLoading: true,
    loadingText: "玩命加载中"
  }), 
  updateDiary(e) {
    this.updateDiaryModel
      .fetch({
        id: this.data.id,  //日程id
        memo: this.data.nav_title,  //头部nav
        title: e.detail.value.title,  //标题
        content: e.detail.value.content,//内容
        time: this.data.earlyTime,  //日程时间
      })
      .then(
        wx.switchTab({
          url: "./schedule",
          success: function (e) {
            var page = getCurrentPages().pop();
            if (page == undefined || page == null) return;
            page.onReady();
          }
        })
      )
      .catch(res => {
        //失败
      })
  },
  formSubmit(e) {
    if(this.data.memo.memo=='早会宣讲'){
      wx.showToast({
        title: '早会宣讲不能修改',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(this.data.id){
      this.updateDiary(e);
    }else{
      this.addDiary(e);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if(this.data.id){
      this.getStaffTeachData();
    }
    wx.stopPullDownRefresh(); //停止下拉
  },
})